import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:xingsu/net/apis.dart';
import 'package:xingsu/net/http_manager.dart';
import 'package:xingsu/page/video/model/Video_model.dart';

class HomeViewModel extends GetxController {
  int page = 0;
  final datas = [].obs;
  final RefreshController refreshController = RefreshController();

  @override
  void onInit() {
    super.onInit();

    loadData();
  }

  void loadData() {
    HttpManager.getInstance().get(Apis.homeList,
        params: {"from": page * 10, "limit": 10}, success: (resp) {
      int code = resp["code"];
      if (refreshController.isRefresh) {
        refreshController.refreshCompleted();
      }

      if (refreshController.isLoading) {
        refreshController.loadComplete();
      }

      if (code == 0) {
        if (page == 0) {
          datas.clear();
        }

        List _datas = [];
        for (Map map in resp["data"] ?? []) {
          VideoModel model = VideoModel.fromJson(map);
          _datas.add(model);
        }

        datas.addAll(_datas);
      }
    });
  }

  void refreshData() {
    page = 0;
    loadData();
  }

  void loadMore() {
    page += 1;
    loadData();
  }
}
